import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Image2HTML {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		System.out.println(image2HTMLString("c:/Digital_composite_spring_4002.jpg"));
		images2javascript("c:/Digital_composite_spring_4002.jpg");
	}

	public static String image2HTMLString(String imagePathString)
			throws IOException {
		FileWriter fileWriter = new FileWriter("c:/tes71.html", true);
		StringBuffer htmlBuffer = new StringBuffer(9000);
		StringBuffer tableBuffer = new StringBuffer();
		int m = 10;
		htmlBuffer
				.append("<style type='text/css'>td{height='1px';width='1px'}</style>");
		htmlBuffer.append("<table  align='center' cellspacing='0' border='0'>");
		fileWriter.write(htmlBuffer.toString());
		try {
			BufferedImage imd = ImageIO.read(new File(imagePathString));

			int heigth = imd.getHeight();
			int width = imd.getWidth();
			for (int i = 0; i < heigth; i = i + m) {
				StringBuffer trStringBuffer = new StringBuffer();
				trStringBuffer.append("<tr>");

				for (int j = 0; j < width; j = j + m) {
					// System.out.println(Integer.toHexString(imd.getRGB(j,
					// i)));
					trStringBuffer.append("<td bgcolor='"
							+ Integer.toHexString(imd.getRGB(j, i)).substring(
									2, 8) + "' >");
					trStringBuffer.append("</td>");
				}
				trStringBuffer.append("</tr>");
				fileWriter.write(trStringBuffer.toString());
				fileWriter.flush();
			}
			// htmlBuffer.append("</table>");
			fileWriter.write("</table>");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return htmlBuffer.toString();
	}

	public static void images2javascript(String imagePathString)
			throws IOException {
		FileWriter fileWriter = new FileWriter("c:/tes91.html", true);
		fileWriter.write("<script>var colors=new Array(");
		int m = 10;
		try {
			BufferedImage imd = ImageIO.read(new File(imagePathString));
			int heigth = imd.getHeight();
			int width = imd.getWidth();
			for (int i = 0; i < heigth; i = i + m) {
				StringBuffer trStringBuffer = new StringBuffer();
				trStringBuffer.append("[");
				for (int j = 0; j < width; j = j + m) {
					trStringBuffer.append("'"
							+ Integer.toHexString(imd.getRGB(j, i)).substring(
									2, 8) + "',");

				}
				trStringBuffer.append("],");
				// System.out.println(trStringBuffer.toString());
				fileWriter.write(trStringBuffer.toString());
			}
			fileWriter.write("[]);");
			fileWriter
					.write("document.write(\"<style type='text/css'>td{height='10px';width='10px'}" +
							"</style><table  align='center' cellspacing='0' border='0'>\");" +
							"for (i=0; i < colors.length; i++)	" +
							"{ " +
								"document.write('<tr>'); " +
								"for (j=0; j < colors[i].length; j++) { " +
									" document.write(\"<td bgcolor='\"+colors[i][j]+\"' >\"); " +
								"} document.write('</tr>');" +
							"}" +
							"document.write('</table>');" +
							"</script>");

			fileWriter.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
